package com.lc.pianostudent.activity;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lc.pianostudent.R;
import com.lc.pianostudent.bean.AnswerResultBean;
import com.lc.pianostudent.conn.PostAnswerReview;
import com.lc.pianostudent.conn.PostGetAnswer;
import com.lc.pianostudent.util.Util;
import com.lc.pianostudent.view.CountDownView;
import com.zcx.helper.bound.BoundView;
import com.zcx.helper.glide.GlideLoader;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilToast;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/7/30.
 */

public class AnswerBackActivity extends BaseActivity implements View.OnClickListener{
    private SharedPreferences sp ;
    @BoundView(R.id.type) private TextView type_text;
    private int total_question_int = 1;
    @BoundView(R.id.title_text) private TextView title_text;
    @BoundView(R.id.title_image) private ImageView title_image;
    @BoundView(value = R.id.total_question) private TextView total_question;
    @BoundView(value = R.id.one_layout,isClick = true) private RelativeLayout one_layout;
    @BoundView(value = R.id.two_layout,isClick = true) private RelativeLayout two_layout;
    @BoundView(value = R.id.three_layout,isClick = true) private RelativeLayout three_layout;
    @BoundView(value = R.id.four_layout,isClick = true) private RelativeLayout four_layout;
    @BoundView(value = R.id.five_layout,isClick = true) private RelativeLayout five_layout;
    @BoundView(R.id.choose_a) private TextView choose_a;
    @BoundView(R.id.choose_b) private TextView choose_b;
    @BoundView(R.id.choose_c) private TextView choose_c;
    @BoundView(R.id.choose_d) private TextView choose_d;
    @BoundView(R.id.choose_e) private TextView choose_e;

    @BoundView(R.id.choose_a_back) private TextView choose_a_back;
    @BoundView(R.id.choose_b_back) private TextView choose_b_back;
    @BoundView(R.id.choose_c_back) private TextView choose_c_back;
    @BoundView(R.id.choose_d_back) private TextView choose_d_back;
    @BoundView(R.id.choose_e_back) private TextView choose_e_back;
    @BoundView(R.id.choose_a_image) private ImageView choose_a_image;
    @BoundView(R.id.choose_b_image) private ImageView choose_b_image;
    @BoundView(R.id.choose_c_image) private ImageView choose_c_image;
    @BoundView(R.id.choose_d_image) private ImageView choose_d_image;
    @BoundView(R.id.choose_e_image) private ImageView choose_e_image;
    @BoundView(R.id.correct_choose) private TextView correct_choose;
    @BoundView(R.id.your_choose) private TextView your_choose;
    private PostAnswerReview.PostAnswerReviewInfo currentInfo;
    @BoundView(R.id.title_text_count) private TextView title_text_count;
    @BoundView(value = R.id.next_question_back,isClick = true) private TextView next_question_back;
    private ArrayList<String> choose_list = new ArrayList<>();
    private List<AnswerResultBean> list;
    private List<ArrayList<String>> list1 = new ArrayList<>();

    //题目接口
    public PostAnswerReview postAnswerReview = new PostAnswerReview(new AsyCallBack<PostAnswerReview.PostAnswerReviewInfo>(){


        @Override
        public void onSuccess(String toast, int type, Object o, PostAnswerReview.PostAnswerReviewInfo postAnswerReviewInfo) throws Exception {
            currentInfo = postAnswerReviewInfo;
            total_question.setText(total_question_int+"");

            String answer = postAnswerReviewInfo.answer.replace(",","");
            correct_choose.setText("正确选项："+answer);
            if(postAnswerReviewInfo.type.equals("4")){ //单选
                one_layout.setVisibility(View.VISIBLE);
                two_layout.setVisibility(View.VISIBLE);
                three_layout.setVisibility(View.VISIBLE);
                four_layout.setVisibility(View.VISIBLE);
                five_layout.setVisibility(View.INVISIBLE);
                type_text.setText("单选");

                if(answer.equals("A")){
                    one_layout.setBackgroundResource(R.drawable.shape_blue_50px);

                    two_layout.setBackgroundResource(R.drawable.shape_circle40_gray_storke);
                    three_layout.setBackgroundResource(R.drawable.shape_circle40_gray_storke);
                    four_layout.setBackgroundResource(R.drawable.shape_circle40_gray_storke);
                    five_layout.setBackgroundResource(R.drawable.shape_circle40_gray_storke);

                    choose_a.setTextColor(Color.parseColor("#ffffff"));
                    choose_a_back.setTextColor(Color.parseColor("#ffffff"));

                    choose_b.setTextColor(Color.parseColor("#424242"));
                    choose_b_back.setTextColor(Color.parseColor("#424242"));

                    choose_c.setTextColor(Color.parseColor("#424242"));
                    choose_c_back.setTextColor(Color.parseColor("#424242"));

                    choose_d.setTextColor(Color.parseColor("#424242"));
                    choose_d_back.setTextColor(Color.parseColor("#424242"));

                    choose_e.setTextColor(Color.parseColor("#424242"));
                    choose_e_back.setTextColor(Color.parseColor("#424242"));
                }

                if(answer.equals("B")){
                    one_layout.setBackgroundResource(R.drawable.shape_circle40_gray_storke);

                    two_layout.setBackgroundResource(R.drawable.shape_blue_50px);
                    three_layout.setBackgroundResource(R.drawable.shape_circle40_gray_storke);
                    four_layout.setBackgroundResource(R.drawable.shape_circle40_gray_storke);
                    five_layout.setBackgroundResource(R.drawable.shape_circle40_gray_storke);

                    choose_a.setTextColor(Color.parseColor("#424242"));
                    choose_a_back.setTextColor(Color.parseColor("#424242"));

                    choose_b.setTextColor(Color.parseColor("#ffffff"));
                    choose_b_back.setTextColor(Color.parseColor("#ffffff"));

                    choose_c.setTextColor(Color.parseColor("#424242"));
                    choose_c_back.setTextColor(Color.parseColor("#424242"));

                    choose_d.setTextColor(Color.parseColor("#424242"));
                    choose_d_back.setTextColor(Color.parseColor("#424242"));

                    choose_e.setTextColor(Color.parseColor("#424242"));
                    choose_e_back.setTextColor(Color.parseColor("#424242"));
                }

                if(answer.equals("C")){
                    one_layout.setBackgroundResource(R.drawable.shape_circle40_gray_storke);

                    two_layout.setBackgroundResource(R.drawable.shape_circle40_gray_storke);
                    three_layout.setBackgroundResource(R.drawable.shape_blue_50px);
                    four_layout.setBackgroundResource(R.drawable.shape_circle40_gray_storke);
                    five_layout.setBackgroundResource(R.drawable.shape_circle40_gray_storke);

                    choose_a.setTextColor(Color.parseColor("#424242"));
                    choose_a_back.setTextColor(Color.parseColor("#424242"));

                    choose_b.setTextColor(Color.parseColor("#424242"));
                    choose_b_back.setTextColor(Color.parseColor("#424242"));

                    choose_c.setTextColor(Color.parseColor("#ffffff"));
                    choose_c_back.setTextColor(Color.parseColor("#ffffff"));

                    choose_d.setTextColor(Color.parseColor("#424242"));
                    choose_d_back.setTextColor(Color.parseColor("#424242"));

                    choose_e.setTextColor(Color.parseColor("#424242"));
                    choose_e_back.setTextColor(Color.parseColor("#424242"));
                }

                if(answer.equals("D")){
                    one_layout.setBackgroundResource(R.drawable.shape_circle40_gray_storke);

                    two_layout.setBackgroundResource(R.drawable.shape_circle40_gray_storke);
                    three_layout.setBackgroundResource(R.drawable.shape_circle40_gray_storke);
                    four_layout.setBackgroundResource(R.drawable.shape_blue_50px);
                    five_layout.setBackgroundResource(R.drawable.shape_circle40_gray_storke);

                    choose_a.setTextColor(Color.parseColor("#424242"));
                    choose_a_back.setTextColor(Color.parseColor("#424242"));

                    choose_b.setTextColor(Color.parseColor("#424242"));
                    choose_b_back.setTextColor(Color.parseColor("#424242"));

                    choose_c.setTextColor(Color.parseColor("#424242"));
                    choose_c_back.setTextColor(Color.parseColor("#424242"));

                    choose_d.setTextColor(Color.parseColor("#ffffff"));
                    choose_d_back.setTextColor(Color.parseColor("#ffffff"));

                    choose_e.setTextColor(Color.parseColor("#424242"));
                    choose_e_back.setTextColor(Color.parseColor("#424242"));
                }

                if(answer.equals("E")){
                    one_layout.setBackgroundResource(R.drawable.shape_circle40_gray_storke);

                    two_layout.setBackgroundResource(R.drawable.shape_circle40_gray_storke);
                    three_layout.setBackgroundResource(R.drawable.shape_circle40_gray_storke);
                    four_layout.setBackgroundResource(R.drawable.shape_circle40_gray_storke);
                    five_layout.setBackgroundResource(R.drawable.shape_blue_50px);

                    choose_a.setTextColor(Color.parseColor("#424242"));
                    choose_a_back.setTextColor(Color.parseColor("#424242"));

                    choose_b.setTextColor(Color.parseColor("#424242"));
                    choose_b_back.setTextColor(Color.parseColor("#424242"));

                    choose_c.setTextColor(Color.parseColor("#424242"));
                    choose_c_back.setTextColor(Color.parseColor("#424242"));

                    choose_d.setTextColor(Color.parseColor("#424242"));
                    choose_d_back.setTextColor(Color.parseColor("#424242"));

                    choose_e.setTextColor(Color.parseColor("#ffffff"));
                    choose_e_back.setTextColor(Color.parseColor("#ffffff"));
                }
            }else if(postAnswerReviewInfo.type.equals("5")){  //多选
                one_layout.setVisibility(View.VISIBLE);
                two_layout.setVisibility(View.VISIBLE);
                three_layout.setVisibility(View.VISIBLE);
                four_layout.setVisibility(View.VISIBLE);
                five_layout.setVisibility(View.VISIBLE);
                type_text.setText("多选");

                if(answer.contains("A")){
                    one_layout.setBackgroundResource(R.drawable.shape_blue_50px);
                    choose_a.setTextColor(Color.parseColor("#ffffff"));
                    choose_a_back.setTextColor(Color.parseColor("#ffffff"));
                }
                if(answer.contains("B")){
                    two_layout.setBackgroundResource(R.drawable.shape_blue_50px);
                    choose_b.setTextColor(Color.parseColor("#ffffff"));
                    choose_b_back.setTextColor(Color.parseColor("#ffffff"));
                }
                if(answer.contains("C")){
                    three_layout.setBackgroundResource(R.drawable.shape_blue_50px);
                    choose_c.setTextColor(Color.parseColor("#ffffff"));
                    choose_c_back.setTextColor(Color.parseColor("#ffffff"));
                }
                if(answer.contains("D")){
                    four_layout.setBackgroundResource(R.drawable.shape_blue_50px);
                    choose_d.setTextColor(Color.parseColor("#ffffff"));
                    choose_d_back.setTextColor(Color.parseColor("#ffffff"));
                }
                if(answer.contains("E")){
                    five_layout.setBackgroundResource(R.drawable.shape_blue_50px);
                    choose_e.setTextColor(Color.parseColor("#ffffff"));
                    choose_e_back.setTextColor(Color.parseColor("#ffffff"));
                }
            }else if(postAnswerReviewInfo.type.equals("2")){  //判断

                one_layout.setVisibility(View.VISIBLE);
                two_layout.setVisibility(View.VISIBLE);
                three_layout.setVisibility(View.INVISIBLE);
                four_layout.setVisibility(View.INVISIBLE);
                five_layout.setVisibility(View.INVISIBLE);
                type_text.setText("判断");

                if(answer.equals("A")){
                    one_layout.setBackgroundResource(R.drawable.shape_blue_50px);

                    two_layout.setBackgroundResource(R.drawable.shape_circle40_gray_storke);
                    three_layout.setBackgroundResource(R.drawable.shape_circle40_gray_storke);
                    four_layout.setBackgroundResource(R.drawable.shape_circle40_gray_storke);
                    five_layout.setBackgroundResource(R.drawable.shape_circle40_gray_storke);

                    choose_a.setTextColor(Color.parseColor("#ffffff"));
                    choose_a_back.setTextColor(Color.parseColor("#ffffff"));

                    choose_b.setTextColor(Color.parseColor("#424242"));
                    choose_b_back.setTextColor(Color.parseColor("#424242"));

                    choose_c.setTextColor(Color.parseColor("#424242"));
                    choose_c_back.setTextColor(Color.parseColor("#424242"));

                    choose_d.setTextColor(Color.parseColor("#424242"));
                    choose_d_back.setTextColor(Color.parseColor("#424242"));

                    choose_e.setTextColor(Color.parseColor("#424242"));
                    choose_e_back.setTextColor(Color.parseColor("#424242"));
                }

                if(answer.equals("B")){
                    one_layout.setBackgroundResource(R.drawable.shape_circle40_gray_storke);

                    two_layout.setBackgroundResource(R.drawable.shape_blue_50px);
                    three_layout.setBackgroundResource(R.drawable.shape_circle40_gray_storke);
                    four_layout.setBackgroundResource(R.drawable.shape_circle40_gray_storke);
                    five_layout.setBackgroundResource(R.drawable.shape_circle40_gray_storke);

                    choose_a.setTextColor(Color.parseColor("#424242"));
                    choose_a_back.setTextColor(Color.parseColor("#424242"));

                    choose_b.setTextColor(Color.parseColor("#ffffff"));
                    choose_b_back.setTextColor(Color.parseColor("#ffffff"));

                    choose_c.setTextColor(Color.parseColor("#424242"));
                    choose_c_back.setTextColor(Color.parseColor("#424242"));

                    choose_d.setTextColor(Color.parseColor("#424242"));
                    choose_d_back.setTextColor(Color.parseColor("#424242"));

                    choose_e.setTextColor(Color.parseColor("#424242"));
                    choose_e_back.setTextColor(Color.parseColor("#424242"));
                }

                if(answer.equals("C")){
                    one_layout.setBackgroundResource(R.drawable.shape_circle40_gray_storke);

                    two_layout.setBackgroundResource(R.drawable.shape_circle40_gray_storke);
                    three_layout.setBackgroundResource(R.drawable.shape_blue_50px);
                    four_layout.setBackgroundResource(R.drawable.shape_circle40_gray_storke);
                    five_layout.setBackgroundResource(R.drawable.shape_circle40_gray_storke);

                    choose_a.setTextColor(Color.parseColor("#424242"));
                    choose_a_back.setTextColor(Color.parseColor("#424242"));

                    choose_b.setTextColor(Color.parseColor("#424242"));
                    choose_b_back.setTextColor(Color.parseColor("#424242"));

                    choose_c.setTextColor(Color.parseColor("#ffffff"));
                    choose_c_back.setTextColor(Color.parseColor("#ffffff"));

                    choose_d.setTextColor(Color.parseColor("#424242"));
                    choose_d_back.setTextColor(Color.parseColor("#424242"));

                    choose_e.setTextColor(Color.parseColor("#424242"));
                    choose_e_back.setTextColor(Color.parseColor("#424242"));
                }

                if(answer.equals("D")){
                    one_layout.setBackgroundResource(R.drawable.shape_circle40_gray_storke);

                    two_layout.setBackgroundResource(R.drawable.shape_circle40_gray_storke);
                    three_layout.setBackgroundResource(R.drawable.shape_circle40_gray_storke);
                    four_layout.setBackgroundResource(R.drawable.shape_blue_50px);
                    five_layout.setBackgroundResource(R.drawable.shape_circle40_gray_storke);

                    choose_a.setTextColor(Color.parseColor("#424242"));
                    choose_a_back.setTextColor(Color.parseColor("#424242"));

                    choose_b.setTextColor(Color.parseColor("#424242"));
                    choose_b_back.setTextColor(Color.parseColor("#424242"));

                    choose_c.setTextColor(Color.parseColor("#424242"));
                    choose_c_back.setTextColor(Color.parseColor("#424242"));

                    choose_d.setTextColor(Color.parseColor("#ffffff"));
                    choose_d_back.setTextColor(Color.parseColor("#ffffff"));

                    choose_e.setTextColor(Color.parseColor("#424242"));
                    choose_e_back.setTextColor(Color.parseColor("#424242"));
                }

                if(answer.equals("E")){
                    one_layout.setBackgroundResource(R.drawable.shape_circle40_gray_storke);

                    two_layout.setBackgroundResource(R.drawable.shape_circle40_gray_storke);
                    three_layout.setBackgroundResource(R.drawable.shape_circle40_gray_storke);
                    four_layout.setBackgroundResource(R.drawable.shape_circle40_gray_storke);
                    five_layout.setBackgroundResource(R.drawable.shape_blue_50px);

                    choose_a.setTextColor(Color.parseColor("#424242"));
                    choose_a_back.setTextColor(Color.parseColor("#424242"));

                    choose_b.setTextColor(Color.parseColor("#424242"));
                    choose_b_back.setTextColor(Color.parseColor("#424242"));

                    choose_c.setTextColor(Color.parseColor("#424242"));
                    choose_c_back.setTextColor(Color.parseColor("#424242"));

                    choose_d.setTextColor(Color.parseColor("#424242"));
                    choose_d_back.setTextColor(Color.parseColor("#424242"));

                    choose_e.setTextColor(Color.parseColor("#ffffff"));
                    choose_e_back.setTextColor(Color.parseColor("#ffffff"));
                }
            }
            title_text_count.setText(total_question_int+"、");
            if(!TextUtils.isEmpty(postAnswerReviewInfo.picurl)){
                title_image.setVisibility(View.VISIBLE);
                GlideLoader.getInstance().get("",postAnswerReviewInfo.picurl,title_image);
            }else {
                title_image.setVisibility(View.GONE);
            }

            title_text.setText(postAnswerReviewInfo.title);

            if(postAnswerReviewInfo.format.equals("1")){

                choose_a.setVisibility(View.VISIBLE);
                choose_b.setVisibility(View.VISIBLE);
                choose_c.setVisibility(View.VISIBLE);
                choose_d.setVisibility(View.VISIBLE);
                choose_e.setVisibility(View.VISIBLE);

                choose_a_image.setVisibility(View.INVISIBLE);
                choose_b_image.setVisibility(View.INVISIBLE);
                choose_c_image.setVisibility(View.INVISIBLE);
                choose_d_image.setVisibility(View.INVISIBLE);
                choose_e_image.setVisibility(View.INVISIBLE);
                choose_a.setText(postAnswerReviewInfo.optionA);
                choose_b.setText(postAnswerReviewInfo.optionB);
                choose_c.setText(postAnswerReviewInfo.optionC);
                choose_d.setText(postAnswerReviewInfo.optionD);
                choose_e.setText(postAnswerReviewInfo.optionE);
            }else {
                choose_a.setVisibility(View.INVISIBLE);
                choose_b.setVisibility(View.INVISIBLE);
                choose_c.setVisibility(View.INVISIBLE);
                choose_d.setVisibility(View.INVISIBLE);
                choose_e.setVisibility(View.INVISIBLE);

                choose_a_image.setVisibility(View.VISIBLE);
                choose_b_image.setVisibility(View.VISIBLE);
                choose_c_image.setVisibility(View.VISIBLE);
                choose_d_image.setVisibility(View.VISIBLE);
                choose_e_image.setVisibility(View.VISIBLE);
                GlideLoader.getInstance().get("",postAnswerReviewInfo.optionApic,choose_a_image);
                GlideLoader.getInstance().get("",postAnswerReviewInfo.optionBpic,choose_b_image);
                GlideLoader.getInstance().get("",postAnswerReviewInfo.optionCpic,choose_c_image);
                GlideLoader.getInstance().get("",postAnswerReviewInfo.optionDpic,choose_d_image);
                GlideLoader.getInstance().get("",postAnswerReviewInfo.optionEpic,choose_e_image);

            }


//
        }

        @Override
        public void onFail(String toast, int type) throws Exception {
            UtilToast.show(toast);
        }
    });
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_answer_back);

        setBackTrue();
        initView();
    }

    private void initView(){
        choose_list = getIntent().getStringArrayListExtra("select");
        total_question_int = Integer.parseInt(getIntent().getStringExtra("count"));

//        UtilToast.show(getIntent().getStringArrayListExtra("select").toString()+"===jy");
        StringBuilder sb=new StringBuilder();
        for(String s:choose_list){//aa就是你想转化的集合
            sb.append(s);
        }
        //首次进入试题回顾的您的选项
        your_choose.setText("您的选项："+sb.toString());
        sp = getSharedPreferences("User", Context.MODE_PRIVATE);
        String data = sp.getString("listStr", "");
        Gson gson = new Gson();
        Type listType = new TypeToken<List<AnswerResultBean>>() {
        }.getType();
        list = gson.fromJson(data, listType);

        String liststr = sp.getString("mylistStr", "");

        if (!TextUtils.isEmpty(liststr)) {
            try {
                list1 = Util.String2SceneList(liststr);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if(total_question_int == 10) {
            next_question_back.setVisibility(View.INVISIBLE);
        }
        postAnswerReview.id = getIntent().getStringExtra("id");
        postAnswerReview.execute();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.next_question_back:
                one_layout.setBackgroundResource(R.drawable.shape_circle40_gray_storke);

                two_layout.setBackgroundResource(R.drawable.shape_circle40_gray_storke);
                three_layout.setBackgroundResource(R.drawable.shape_circle40_gray_storke);
                four_layout.setBackgroundResource(R.drawable.shape_circle40_gray_storke);
                five_layout.setBackgroundResource(R.drawable.shape_circle40_gray_storke);

                choose_a.setTextColor(Color.parseColor("#424242"));
                choose_a_back.setTextColor(Color.parseColor("#424242"));

                choose_b.setTextColor(Color.parseColor("#424242"));
                choose_b_back.setTextColor(Color.parseColor("#424242"));

                choose_c.setTextColor(Color.parseColor("#424242"));
                choose_c_back.setTextColor(Color.parseColor("#424242"));

                choose_d.setTextColor(Color.parseColor("#424242"));
                choose_d_back.setTextColor(Color.parseColor("#424242"));

                choose_e.setTextColor(Color.parseColor("#424242"));
                choose_e_back.setTextColor(Color.parseColor("#424242"));
//                Log.e("---total",total_question_int+"==JY");

                total_question_int++;
                if(total_question_int == 10) {
                    next_question_back.setVisibility(View.INVISIBLE);
                }

                choose_list = list1.get(total_question_int - 1);

                StringBuilder sb = new StringBuilder();
                for (String s : choose_list) {//aa就是你想转化的集合
                    sb.append(s);
                }

                your_choose.setText("您的选项：" + sb.toString());

                postAnswerReview.id = list.get(total_question_int - 1).getId();
                postAnswerReview.execute();
                break;

        }
    }
}
